package cn.ztf.zshop.sys.controller;

import cn.ztf.zshop.sys.po.Role;
import cn.ztf.zshop.sys.vo.RoleVo;
import cn.ztf.zshop.sys.service.SysRoleService;
import cn.ztf.zshop.sys.dto.RoleDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色管理
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    /**
     * 分页查询角色
     *
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<Page<RoleVo>> page(Page<Role> page) {
        Page<RoleVo> list = sysRoleService.pageRole(page);
        return ResponseEntity.ok(list);
    }

//    /**
//     * 查询全部角色
//     * @return
//     */
//    @GetMapping("/list")
//    public ResponseEntity<List<RoleVo>> list() {
//        List<RoleVo> list = sysRoleService.listRole();
//        return ResponseEntity.ok(list);
//    }

    /**
     * 根据角色id查询角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("{roleId}")
    public ResponseEntity<RoleVo> get(@PathVariable Long roleId) {
        RoleVo roleVo = sysRoleService.getRoleById(roleId);
        return ResponseEntity.ok(roleVo);
    }

    /**
     * 添加角色信息
     *
     * @param roleDto
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> create(@Validated @RequestBody RoleDto roleDto) {
        sysRoleService.create(roleDto);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改角色信息
     *
     * @param roleDto
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> update(@Validated(RoleDto.Update.class) @RequestBody RoleDto roleDto) {
        sysRoleService.updateRole(roleDto);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        sysRoleService.delete(id);
        return ResponseEntity.ok().build();
    }

//    /**
//     * 查询全部角色
//     *
//     * @return
//     */
//    public ResponseEntity<List<RoleVo>> list() {
//        List<RoleVo> list = sysRoleService.listRole();
//        return ResponseEntity.ok(list);
//    }

}
